﻿
using System;
using System.Data;
using System.Collections.Generic;
using BYFW.SFL;

namespace LDSP.BRL
{
    public partial class SystemUser : BYFW.BOL.BaseConfigEntity
    {
        public SystemUser()
        {
            m_tableName = "t_sysuser";
            m_hasCode = true;
            m_hasName = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
        }

        protected string m_password = string.Empty;
        protected string m_module = string.Empty;

        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get
            {
                if (m_password.Length > 1)
                {
                    return CommonOP.DecryptString(m_password.Substring(1), false);
                }
                else
                {
                    return m_password;
                }
            }
            set
            {
                m_password = CommonOP.Spliter + CommonOP.EncryptString(value);
            }
        }
        /// <summary>
        /// 授权的模块
        /// </summary>
        public string Module
        {
            get { return m_module; }
            set { m_module = value; }
        }

        /// <summary>
        /// 是否是超级用户
        /// </summary>
        public bool IsSuper
        {
            get;
            set;
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("password", m_password);
            m_container.Add("module", m_module);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_password = this.GetStringValue(record, "password");
            m_module = this.GetStringValue(record, "module");
            return true;
        }

        public override int Delete(string transID)
        {
            new R_SystemUserCatalog().DeleteBySystemUser(transID, m_id);
            return base.Delete(transID);
        }

        public void UpdatePwdToEncrypted()
        {
            string cond = string.Format("WHERE {0} not like N'{1}%'", DB.GetCol("password"), CommonOP.Spliter);
            var list = this.GetSpecialList<SystemUser>(null, cond);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Password = list[i].m_password;
                list[i].Update();
            }
        }

        /// <summary>
        /// 获取列表形式的模块
        /// </summary>
        /// <returns></returns>
        public List<string> GetModuleList()
        {
            List<string> res = new List<string>();
            if (!string.IsNullOrEmpty(m_module))
            {
                string[] tmp = m_module.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                res.AddRange(tmp);
            }
            return res;
        }
    }
}
